#include "FireworkManager.h"

#include "Firework.h"
#include "DataSingleton.h"

void FireworkManager::setFireworkCount(uint8_t fireworkCount) {
	m_fireworkCount = fireworkCount;

	std::uniform_real_distribution<double> posDis{ -20, 50 };
	std::uniform_real_distribution<double> speedDis{ 30 ,50 };
	auto& gen = DataSingleton::instance()->m_gen;
	for (int i = 0; i < m_fireworkCount; ++i) {
		vsg::dvec3 pos{ posDis(gen), posDis(gen), posDis(gen) };
		double speed{ speedDis(gen) };
		uint32_t particleCount = 2500;
		auto firework = std::make_shared<Firework>(pos, speed, particleCount);
		firework->init();
		firework->activate();
		m_fireworks.push_back(firework);
	}
}

void FireworkManager::update(std::chrono::nanoseconds delt) {
	for (auto& firework : m_fireworks) {
		firework->update(delt);
		if (firework->isDone()) {
			std::uniform_real_distribution<double> posDis{ -20, 50 };
			std::uniform_real_distribution<double> speedDis{ 30 ,50 };
			auto& gen = DataSingleton::instance()->m_gen;
			vsg::dvec3 pos{ posDis(gen), posDis(gen), 0. };
			double speed{ speedDis(gen) };
			firework->reset(pos, speed);
			firework->activate();
		}
	}
}

vsg::ref_ptr<vsg::Node> FireworkManager::getNode()
{
	auto scene = vsg::Group::create();
	for (auto firework : m_fireworks) {
		scene->addChild(firework->getNode());
	}
	return scene;
}
